In C, a variable must be declared
before it can be used. Variables can be declared at the start of an block of
code, but most are found at the start of each function. Most local variables
appear when the function is called, and are destroyed on return from that
function.
A declaration begins with the
type, followed by the name of one or more variables.
Declarations can also be spread
out. This allows space for an explanatory comment to be added. Variables can
also be initialised when they are declared, this is done by adding an equals
sign and the required value after the declaration.
int high = 250;
Variable high is declared as
integer and is initialised to 250.
All of the integer types plus the
char are called the integral types. float and double are called the real
types.
Every variable has a name and a
value. The name is used to identify the variable, the value is used to store
data. There is a limitation on what these names can be. Every variable name in
C must start with a letter, the rest of the name can consist of letters,
numbers and underscore characters. C recognises upper and lower case
characters as being different. Finally, you cannot use any of C's keywords
like main, while, switch etc as variable names.
It is conventional to avoid the
use of capital letters in variable names. These are used for names of
constants. Some old implementations of C only use the first 8 characters of a
variable name. Most modern ones don't apply this limit though.
The rules governing variable
names also apply to the names of functions. We shall meet functions later on
in the course.
For reasons not explained here,
variables can only store finite numbers. Suppose that the size of a particular
data type, that we'll call a
gorb, is 1
byte. That
means that gorbs can only represent 2
8*1 = 2
8 = 256
distinct values. So, gorbs might be able to store only the numbers between 0
and 255 (inclusive). Any number that you tried to store in a gorb which was smaller
than 0, or larger than 255, would not be stored correctly; it would be stored
as one of the values between 0 and 255. However, maybe you want to be able to
store positive and negative numbers in gorbs, in which case you'd only be able
to store 128 negative numbers and 128 positive numbers. Since we need to be
able to store 0 also, you might decide that the range of values for a gorb is
-128 to 127.
We've already learned about two
different data types. What are the sizes of these data types, and what are the
limits on the kinds of values that they can store? We just saw that a data
type whose size is 1 byte can store 256 distinct values. Data types of size 2
bytes can store 28*2 = 216 = 65536 different values. Using the same
formula, we determine that data types of size 4 bytes can store 28*4 = 232 = 4,294,967,296.
Unfortunately, the size of data
types like int and float are not standard across all
systems. The size of an int
depends on your operating system and your hardware. Here are some typical
values for ints and floats, along with some other
important data types.
type -- typical
size -- description
short
-- 2 bytes -- stores a short (i.e., small)
integer
int
-- 4 bytes -- stores an integer
long
-- 4 bytes -- stores a long (i.e., large)
integer
float
-- 4 bytes -- stores a floating-point
number
double
-- 8 bytes -- stores a
"double-precision" floating-point number